<?php

$objXajax->registerFunction("cusfrmimportproduct_loadGrid");
$objXajax->registerFunction("cusfrmimportproduct_edit");
$objXajax->registerFunction("cusfrmimportproduct_save");
$objXajax->registerFunction("cusfrmimportproduct_init");
//$objXajax->registerFunction("cusfrmimportproduct_type");
    
function cusfrmimportproduct_loadGrid(){     

    global $lang;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $arrGrid = cusfrmimportproduct_getGrid();
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/customer/cus_importproduct_grid.html');
    $oTbs->MergeBlock('blk_data', $arrGrid);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('cusfrmimportproduct_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTable('cusfrmimportproduct_tbl');");
    $objResponse->addScript('changeWaiting(1);');                              
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}

function  cusfrmimportproduct_save($params){
    global $objDbSelect,$user_id,$tbl_postfix,$hotel_id;
    $objResponse = new xajaxResponse();
    $quanlity = $params['txtquanlity'] ;
    $money = $params['txtmoney'] ;
    $product_id = $params['cbbproduct'] ;
    $description = $params['txtdescription'] ;
    $edit = $params['hdEdit'];
    $id= $params['hdId'];
    $sql='';
    if($edit==1){
        $sql = "UPDATE cus_importproduct SET money='$money',
        description='$description',date_update=now(),user_update='$user_id',quanlity='$quanlity',product_id='$product_id'
        WHERE id ='$id' and service_id = '$hotel_id' ";
    }else{
         $sql = "INSERT INTO cus_importproduct(money,description,date_created,user_created,date_update,user_update,quanlity,product_id,service_id) 
                                value('$money','$description',now(),'$user_id',now(),'$user_id','$quanlity','$product_id','$hotel_id')";   
    }
   //echo $sql; die();
   if($objDbSelect->Execute($sql)){
       $objResponse->addScript("checkUpdateStatus(1,'cusfrmimportproduct','$edit');");
       $objResponse->addScript('xajax_cusfrmimportproduct_loadGrid();'); 
        }else{
       $objResponse->addScript("checkUpdateStatus(0,'cusfrmimportproduct','$edit');");
    }
/*    $objDbSelect->Execute($sql);
    $objResponse->addScript('xajax_cusfrmimportproduct_loadGrid();');
    $objResponse->addScript('changeWaiting(1);');*/
    return $objResponse->getXML(); 
} 
  
function  cusfrmimportproduct_getGrid(){
    global $objDbSelect,$lang,$tbl_postfix,$user_id,$hotel_id;
    $sqlSelect = " SELECT c.*,datediff(now(),c.date_created) as today ,pr.name_vi  as productname,u.name_vi as unitname
    FROM cus_importproduct c left join cus_product$tbl_postfix as pr on  
    c.product_id = pr.id and c.service_id = $hotel_id left join  cus_unit as u on u.id = pr.unit WHERE  datediff(now(),c.date_created)<30 and
     c.user_created = $user_id and c.service_id = $hotel_id order by c.date_created desc";
     //echo $sqlSelect; die();
    return $objDbSelect->GetArray($sqlSelect);
}  

function  cusfrmimportproduct_edit($id){
    global $objDbSelect,$tbl_postfix,$hotel_id;
    $sql = " SELECT * FROM cus_importproduct WHERE id = '$id' and service_id = '$hotel_id'";
    $arr = $objDbSelect->GetArray($sql);
    $objResponse = new xajaxResponse();
    $quanlity =FormatMoney($arr[0]['quanlity']) ;
    $money = FormatMoney($arr[0]['money']);
    $product_id = $arr[0]['product_id'];

    
    $description =htmlspecialchars_decode($arr[0]['description']) ;
    $objResponse->addAssign('cusfrmimportproduct_txtdescription','value',$description);
    
    $objResponse->addScript("$('#cusfrmimportproduct_txtquanlity').val('$quanlity');");
    $objResponse->addScript("$('#cusfrmimportproduct_cbbproduct').val('$product_id');");
    $objResponse->addScript("$('#cusfrmimportproduct_txtmoney').val('$money');");
   // $objResponse->addScript("$('#cusfrmimportproduct_txtdescription').val('$description');");
   // $objResponse->addAssign('cusfrmimportproduct_description', 'innerHTML', "'$description'");
    $objResponse->addScript('changeWaiting(1);');
    return $objResponse->getXML();
} 

function  cusfrmimportproduct_init(){
    global $tbl_postfix,$lang;
   $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax      
   $strProduct = createCombobox('cus_product'.$tbl_postfix,array('id','name_'.$lang),
   array('name'=>'name_'.$lang,'value'=>'id'),'cusfrmimportproduct_cbbproduct',false,' WHERE checkquanlity=1 and id<>parent_id');
   $objResponse->addAssign('cusfrmimportproduct_tdproduct', 'innerHTML', $strProduct);
   return $objResponse->getXML(); // dong luon luon co khi goi ham ajax    
}


# end region process data
?>
